
const Koa = require('koa');
const KoaStatic = require('koa-static');
const KoaBody = require('koa-body');
const Session = require('koa-session');
let { Port, staticDir } = require('./config');
let app = new Koa();
const error = require('./app/middleware/error');
app.use(error);
const rewriteUrl = require('./app/middleware/rewriteUrl');
app.use(rewriteUrl);
app.use(KoaStatic(staticDir));
const CONFIG = require('./app/middleware/session');
app.keys = ['session app keys'];
app.use(Session(CONFIG, app));
const isLogin = require('./app/middleware/isLogin');
app.use(isLogin);
app.use(async (ctx, next) => {
  ctx.state.user = ctx.session.user;
  await next();
});
const koaBodyConfig = require('./app/middleware/koaBodyConfig');
app.use(KoaBody(koaBodyConfig));
const Routers = require('./app/routers');
app.use(Routers.routes()).use(Routers.allowedMethods());
app.listen(Port, () => {
  console.log(`服务器启动在${ Port }端口`);
});